package client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class VSM_ConfigScreen extends JDialog implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2625384966813657793L;
	
	private final JButton okButton = new JButton("OK");
	private final JButton cancelButton = new JButton("Cancel");
	
	private final JPanel panelBack = new JPanel();
	private final JPanel panelButton = new JPanel();
	
	private final JLabel labelServerId = new JLabel("Server name or IP address");
	private final JLabel labelPortNum = new JLabel("TCP port at server");
	private final JLabel labelSensorId = new JLabel("Sensor name");
	private final JLabel labelSensorUnit = new JLabel("Sensor unit");
	private final JLabel labelMinValue = new JLabel("Minimum value (default = 0.0)");
	private final JLabel labelMaxValue = new JLabel("Maximum value [1.0,10000.0]");
	private final JLabel labelInterval = new JLabel("Time interval (>=500)");
	
	private final JTextField editServerId = new JTextField();
	private final JTextField editPortNum = new JTextField();
	private final JTextField editSensorId = new JTextField();
	private final JTextField editSensorUnit = new JTextField();
	private final JTextField editMinValue = new JTextField();
	private final JTextField editMaxValue = new JTextField();
	private final JTextField editInterval = new JTextField();
	
	private final VSM_Sensor caller;
	
	VSM_ConfigScreen(Window owner, String title)
	{
		super(owner,title);
		caller = (VSM_Sensor)owner;
		
		GridLayout layout = new GridLayout(7,2);
		panelBack.setLayout(layout);
		//JPanel topPane = new JPanel(layout);
		//JPanel bottomPane = new JPanel();
		
		//this.setSize(400, 250);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		this.setLayout(new BorderLayout());
		this.setResizable(false);
		
		this.add(panelBack,BorderLayout.CENTER);
		this.add(panelButton,BorderLayout.SOUTH);
		
		panelBack.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray),"Visual Sensor Monitor"));
		panelBack.setBackground(Color.white);
		
		panelBack.add(labelServerId);
		panelBack.add(editServerId);
		editServerId.setText(caller.getServerID());
		
		panelBack.add(labelPortNum);
		panelBack.add(editPortNum);
		editPortNum.setText(Integer.toString(caller.getPortNumber()));
		
		panelBack.add(labelSensorId);
		panelBack.add(editSensorId);
		editSensorId.setText(caller.getSensorId());
		
		panelBack.add(labelSensorUnit);
		panelBack.add(editSensorUnit);
		editSensorUnit.setText(caller.getSensorUnit());
		
		panelBack.add(labelMinValue);
		panelBack.add(editMinValue);
		editMinValue.setText(Double.toString(caller.getSensorMinValue()));
		
		panelBack.add(labelMaxValue);
		panelBack.add(editMaxValue);
		editMaxValue.setText(Double.toString(caller.getSensorMaxValue()));
		
		panelBack.add(labelInterval);
		panelBack.add(editInterval);
		editInterval.setText(Integer.toString(caller.getTimeInterval()));
		
		panelButton.add(okButton);
		panelButton.add(cancelButton);
		
		this.setSize(getPreferredSize().width + 100, getPreferredSize().height + 25);
		this.setLocationRelativeTo(null);
		
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == okButton)
		{
			caller.setServerID(editServerId.getText());
			caller.setPortNumber(Integer.parseInt(editPortNum.getText()));
			caller.setSensorId(editSensorId.getText());
			caller.setSensorUnit(editSensorUnit.getText());
			caller.setSensorMinValue(Double.parseDouble(editMinValue.getText()));
			caller.setSensorMaxValue(Double.parseDouble(editMaxValue.getText()));
			caller.setTimeInterval(Integer.parseInt(editInterval.getText()));
			this.dispose();
		}
		
		else if(e.getSource() == cancelButton)
		{
			this.dispose();
		}

	}

}
